package com.xlf.service.blog.controller;
import com.xlf.common.core.annotation.Anonymous;
import com.xlf.common.core.pojo.mode.AjaxResult;
import com.xlf.common.core.pojo.mode.BaseController;
import com.xlf.common.core.util.BeanCopyUtils;
import com.xlf.service.blog.domain.Friend;
import com.xlf.service.blog.domain.dto.AddFriendDTO;
import com.xlf.service.blog.domain.dto.EditFriendDTO;
import com.xlf.service.blog.service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/blog/friend")
public class FriendController extends BaseController {

    @Autowired
    FriendService friendService;


    @Anonymous
    @GetMapping("/list")
    public AjaxResult list(){
        return success(friendService.list());
    }

    @Anonymous
    @GetMapping("/query/{id}")
    public AjaxResult query(@PathVariable Long id){
        return success(friendService.getById(id));
    }

    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('blog:friend:add')")
    public AjaxResult add(@RequestBody AddFriendDTO friendDTO){

        Friend friend = BeanCopyUtils.copyObject(friendDTO, Friend.class);
        return toAjax(friendService.save(friend));
    }

    @PutMapping("/update")
    @PreAuthorize("@ss.hasPermi('blog:friend:update')")
    public AjaxResult update(@RequestBody EditFriendDTO friendDTO){
        Friend friend = BeanCopyUtils.copyObject(friendDTO, Friend.class);

        return toAjax(friendService.updateById(friend));
    }

    @DeleteMapping("/delete/{id}")
    @PreAuthorize("@ss.hasPermi('blog:friend:delete')")
    public AjaxResult delete(@PathVariable Long id){
        return toAjax(friendService.removeById(id));
    }

}
